/**
 * lilang
 * BulletinServiceImpl.java
 * 2013-10-13下午5:37:09
 */
package com.cq.oa.service.pof.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.service.impl.BaseServiceImpl;
import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.util.Page;

import com.cq.oa.dao.pof.BulletinDao;
import com.cq.oa.dao.pof.BulletinReadDao;
import com.cq.oa.dao.pof.BulletinScopelDao;
import com.cq.oa.dao.sm.FileDao;
import com.cq.oa.domain.dto.BulletinDto;
import com.cq.oa.domain.pof.Bulletin;
import com.cq.oa.domain.pof.BulletinRead;
import com.cq.oa.domain.pof.BulletinScope;
import com.cq.oa.domain.utils.NewsUtils;
import com.cq.oa.service.pof.BulletinService;
import com.cq.oa.utils.StringliptUtil;

/**
 * @author lilang
 *
 */
public class BulletinServiceImpl extends BaseServiceImpl<Bulletin> implements BulletinService{
	
	private BulletinDao bulletinDao;
	
	private BulletinReadDao bulletinReadDao;
	
	private BulletinScopelDao bulletinScopelDao;
	
	private FileDao fileDao;

	public void setFileDao(FileDao fileDao) {
		this.fileDao = fileDao;
	}

	public BulletinDao getBulletinDao() {
		return bulletinDao;
	}

	public void setBulletinDao(BulletinDao bulletinDao) {
		this.bulletinDao = bulletinDao;
	}

	public BulletinReadDao getBulletinReadDao() {
		return bulletinReadDao;
	}

	public void setBulletinReadDao(BulletinReadDao bulletinReadDao) {
		this.bulletinReadDao = bulletinReadDao;
	}

	public BulletinScopelDao getBulletinScopelDao() {
		return bulletinScopelDao;
	}

	public void setBulletinScopelDao(BulletinScopelDao bulletinScopelDao) {
		this.bulletinScopelDao = bulletinScopelDao;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.pof.BulletinService#doInsertNews(com.cq.oa.domain.pof.Bulletin, java.lang.String, java.lang.String)
	 */
	public Long doInsertNews(BulletinDto dto) {
		// TODO Auto-generated method stub
		bulletinDao.initBase(dto.getBulletin());
		//创建人
		dto.getBulletin().setCreateUser(SessionManger.getCname());
		//1 拟稿状态  2 发布状态  3停用下架状态
		dto.getBulletin().setNewTypes(1);
		Long sid = bulletinDao.create(dto.getBulletin());
		List<BulletinScope> scopes = this.bulidSocpe(sid, dto.getOperation(), dto.getScopes(), dto.getScopeCnames());
		this.bulletinScopelDao.saveAll(scopes);
		this.fileDao.updateParams(sid, dto.getFileIds());
		return sid;
	}
	/**
	 * 构建查看范围
	 * @param types
	 * @param ids
	 * @param names
	 * @return
	 * lilang 
	 * 2013-10-13下午6:14:33
	 */
	private List<BulletinScope> bulidSocpe(Long sid,int types,String ids,String names){
		List<BulletinScope> scopes = new ArrayList<BulletinScope>();
		if(NewsUtils.SOCPE_COMPANY==types){
			BulletinScope scope = new BulletinScope();
			scope.setObjectTypes(types);
			scope.setObjectSid(SessionManger.getOrgId());
			scope.setObjectCname(SessionManger.getOrgCname());
			scope.setPofBulletinSid(sid);
			this.bulletinScopelDao.initBase(scope);
			scopes.add(scope);
		}else{
			List<Long> sids = StringliptUtil.splitString(ids);
			String[] strings=StringliptUtil.split(names);
			for(int i=0;i<sids.size();i++){
				BulletinScope scope = new BulletinScope();
				scope.setObjectTypes(types);
				scope.setObjectSid(sids.get(i));
				scope.setObjectCname(strings[i]);
				scope.setPofBulletinSid(sid);
				this.bulletinScopelDao.initBase(scope);
				scopes.add(scope);
			}
		}
		return scopes;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.pof.BulletinService#findPageByParams(java.util.Map, int, int)
	 */
	@SuppressWarnings("unchecked")
	public Page<Bulletin> findPageByParams(Map<String, Object> filter,int pageNo, int pageSize) {
		StringBuffer flt=new StringBuffer("select * from pof_bulletin ");
		String where = bulletinDao.toSqlString(filter);
		flt.append(where);
		flt.append(" and sid in (select pof_bulletin_sid from pof_bulletin_scope where ");
		//全单位
		flt.append(" (object_types=1 and org_sid="+SessionManger.getOrgId()+")");
		//部门
		flt.append(" or (object_types=2 and object_sid in (select dept_sid from sm_user_org where user_sid="+SessionManger.getUserId()+"))");
		//岗位角色
		flt.append(" or (object_types=3 and object_sid in (select role_sid from sm_user_role where user_sid="+SessionManger.getUserId()+"))");
		//人员
		flt.append(" or (object_types=4 and object_sid="+SessionManger.getUserId()+")");
		flt.append(")");
		System.out.println(flt.toString());
		return bulletinDao.pagedSql(flt.toString(), pageNo, pageSize, Bulletin.class);
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.pof.BulletinService#updateNews(com.cq.oa.domain.dto.BulletinDto)
	 */
	public void updateNews(BulletinDto dto) {
		// TODO Auto-generated method stub
		bulletinDao.initBase(dto.getBulletin());
		bulletinDao.save(dto.getBulletin());
		List<BulletinScope> scopes = this.bulidSocpe(dto.getBulletin().getSid(), dto.getOperation(), dto.getScopes(), dto.getScopeCnames());
		Map<String,Object> maps = new HashMap<String, Object>();
		maps.put("pofBulletinSid", dto.getBulletin().getSid());
		this.bulletinScopelDao.remove(maps);
		this.bulletinScopelDao.saveAll(scopes);
		this.fileDao.updateParams(dto.getBulletin().getSid(), dto.getFileIds());
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.pof.BulletinService#findByBulletin(java.lang.Long)
	 */
	public BulletinDto findByBulletin(Long sid) {
		BulletinDto dto = new BulletinDto();
		dto.setBulletin(this.bulletinDao.get(sid));
		dto.setSmFiles(this.fileDao.findByBusiness(sid, "News"));
		Map<String,Object> maps = new HashMap<String, Object>();
		maps.put("pofBulletinSid", sid);
		maps.put("orgSid", SessionManger.getOrgId());
		dto.setBulletinScopes(this.bulletinScopelDao.find(maps));
		return dto;
	}

	/* (non-Javadoc)
	 * @see com.cq.oa.service.pof.BulletinService#doViewBulletin(java.lang.Long)
	 */
	public BulletinDto doViewBulletin(Long sid) {
		this.bulidReadNews(sid);
		BulletinDto dto = this.findByBulletin(sid);
		dto.setReads(this.findReads(sid));
		return dto;
	}
	/**
	 * 查看人记录详情
	 * @param sid
	 * lilang 
	 * 2013-10-15上午11:31:34
	 */
	private void bulidReadNews(Long sid){
		Map<String,Object> filter = new HashMap<String, Object>();
		filter.put("pofBulletinSid", sid);
		filter.put("orgSid", SessionManger.getOrgId());
		filter.put("objectSid", SessionManger.getUserId());
		Long count = bulletinReadDao.findByCount(filter, BulletinRead.class);
		if(count==0L){
			BulletinRead bulletinRead = new BulletinRead();
			bulletinRead.setPofBulletinSid(sid);
			bulletinRead.setObjectSid(SessionManger.getUserId());
			bulletinRead.setObjectCname(SessionManger.getCname());
			bulletinRead.setViewTime(new Date());
			//bulletinReadDao.initBase(bulletinRead);
			bulletinReadDao.create(bulletinRead);
		}
	}
	private List<BulletinRead> findReads(Long sid){
		Map<String,Object> filter = new HashMap<String, Object>();
		filter.put("pofBulletinSid", sid);
		filter.put("orgSid", SessionManger.getOrgId());
		Map<String,Object> sort = new HashMap<String, Object>();
		sort.put(Constants.ORDER_BY_ASC, "viewTime");
		return bulletinReadDao.find(filter,sort);
	}
	
}
